package com.franklin.validation.core.argument.impl.collection;

import com.franklin.validation.core.annotations.ParamCheck;
import com.franklin.validation.core.argument.impl.AbstractResolverResult;
import com.franklin.validation.core.argument.impl.string.AbstractStringResolverResult;
import com.franklin.validation.core.argument.impl.string.NumberResolverResult;
import com.franklin.validation.core.entity.CheckFormEntity;
import com.franklin.validation.core.entity.CheckResult;
import com.franklin.validation.core.utils.DataCheckUtil;
import com.franklin.validation.core.utils.ReflectionUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/15 16:30
 */
public abstract class CollectionResolverResult<R extends Collection> extends AbstractStringResolverResult<R>{

    @Autowired
    private NumberResolverResult numberResolverResult;

    @Override
    public CheckResult check(CheckFormEntity input) {
        ParamCheck paramCheck = input.getParamCheck();
        Class<?> paramType = input.getParamType();
        if (paramCheck.type() == Date.class){
            paramType = Date.class;
        }
        CheckResult<List> checkResult = new CheckResult<>();
        checkResult.setSuccess(false);
        checkResult.setParam(paramCheck.value());
        List<String> values = input.getValues();
        boolean isEmpty = DataCheckUtil.isEmpty(values);
        boolean lengthPass = this.checkSize(input);
        if (isEmpty || !lengthPass){
            checkResult.setMessage(this.getErrorMessage(paramCheck));
        }else {
            boolean isPassed = true;
            //遍历检查
            for (String value : values) {
                CheckFormEntity formEntity = new CheckFormEntity(Arrays.asList(value), paramType, paramCheck,input.getGenericType());
                CheckResult parentCheckResult = super.check(formEntity);
                if (!parentCheckResult.isSuccess()){
                    checkResult.setMessage(this.getErrorMessage(paramCheck));
                    isPassed = false;
                    break;
                }
            }
            if (isPassed){
                checkResult.setSuccess(true);
            }
        }
        return checkResult;
    }

    @Override
    public boolean checkSize(CheckFormEntity input) {
        ParamCheck paramCheck = input.getParamCheck();
        List<String> values = input.getValues();
        if (DataCheckUtil.isEmpty(values)){
            if (paramCheck.min() > 0){
                return false;
            }
            return true;
        }
        return paramCheck.max() >= values.size() && paramCheck.min() <= values.size();
    }

    protected Stream<Number> parseNumber(CheckFormEntity formEntity){
        return formEntity.getValues().stream()
                .map(value -> {
                    ParamCheck paramCheck = formEntity.getParamCheck();
                    CheckFormEntity entity = new CheckFormEntity(Arrays.asList(value), formEntity.getGenericType(), paramCheck,formEntity.getGenericType());
                    return this.numberResolverResult.getResult(entity);
                });
    }
}
